/* ************************************************************************
 * Copyright (c) 2018 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * ************************************************************************ */

#include "def.hpp"
#include <stdlib.h>

#include "time_functions.hpp"
#include "../base/backend_manager.hpp"

// the default OS is Linux

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || \
    defined(__WIN64) && !defined(__CYGWIN__)
// Windows
#include <windows.h>

#else
// Linux
#include <sys/time.h>

#endif

namespace rocalution {

double rocalution_time(void)
{
    double the_time_now = 0.0;

    _rocalution_sync();
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || \
    defined(__WIN64) && !defined(__CYGWIN__)
    // Windows
    LARGE_INTEGER now;
    LARGE_INTEGER freq;

    QueryPerformanceCounter(&now);
    QueryPerformanceFrequency(&freq);

    the_time_now = (now.QuadPart * 1000000.0) / static_cast<double>(freq.QuadPart);
#else
    // Linux
    struct timeval now;

    gettimeofday(&now, NULL);
    the_time_now = now.tv_sec * 1000000.0 + (now.tv_usec);
#endif
    return the_time_now;
}

} // namespace rocalution
